home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PASCAL / 0187.ZIP / ARTILLRY.PAS < prev    next >
Pascal/Delphi Source File  |  1985-01-20  |  3KB  |  115 lines

  1. (*
  2.  * artillery
  3.  * fire a shell at an enemy outpost
  4.  *)
  5.  
  6. program artillery (input, output);
  7.  
  8. const
  9.    numshells  =   10;   (* allowed 10 shells per target *)
  10.    mindist    =  100;   (* minimum distance for a target *)
  11.    maxdist    = 1000;   (* maximum distance for a target *)
  12.  
  13.    velocity   = 200.0;  (* initial velocity of 200 ft/sec^2 *)
  14.    gravity    = 32.2;   (* gravity of 32.2 ft/sec^2 *)
  15.    pi         = 3.14159;
  16.  
  17. var
  18.    angle  : real;       (* angle to shoot at *)
  19.    enemy  : integer;    (* how far away the enemy is *)
  20.    killed : integer;    (* how many we have hit *)
  21.    shots  : 0..numshells; (* number of shells left *)
  22.    ch     : char;       (* used to answer questions *)
  23.    hit    : boolean;    (* whether the enemy has been hit *)
  24.  
  25. (*
  26.  * dist
  27.  * returns how far the shell went
  28.  *)
  29.  
  30. function dist:integer;
  31.  
  32. (*
  33.  * timeinair
  34.  * figures out how long the shell
  35.  * stays in the air
  36.  *)
  37.  
  38. function timeinair:real;
  39.  
  40. begin
  41.    timeinair := (2*velocity * sin(angle))/gravity
  42. end;
  43.  
  44. begin
  45.    dist := round((velocity * cos(angle))*timeinair)
  46. end;
  47.  
  48. (*
  49.  * fire
  50.  * the user fires at enemy
  51.  *)
  52.  
  53. procedure fire;
  54.  
  55. begin
  56.    randomize;
  57.    enemy := mindist + random(maxdist-mindist);
  58.    writeln('The enemy is ',enemy:3,' feet away!!!');
  59.    shots := numshells;
  60.    repeat
  61.       write('What angle? ');
  62.       readln(angle);
  63.       angle := (angle * pi)/180.0;
  64.       hit := abs(enemy-dist) <= 1;
  65.       if hit then
  66.       begin
  67.          killed := killed + 1;
  68.          writeln('You hit him!!!');
  69.          writeln('It took you ',numshells-shots,' shots.');
  70.          if killed = 1 then
  71.              writeln('You have killed one enemy.')
  72.          else
  73.              writeln('You have now destroyed ',killed,' enemies of democracy.')
  74.       end
  75.       else
  76.       begin
  77.          shots := shots - 1;
  78.          if dist > enemy then
  79.             write('You overshot by ')
  80.          else
  81.             write('You under shot by ');
  82.          writeln(abs(enemy-dist))
  83.       end
  84.    until (shots = 0) or hit;
  85.    if shots = 0 then
  86.       writeln('You have run out of ammo.')
  87. end;
  88.  
  89. begin
  90.    writeln('Welcome to artillery');
  91.    writeln;
  92.    writeln('You are in the middle of a war (depressing, no?) and are being');
  93.    writeln('charged by thousands of enemies.');
  94.    writeln('Your job is to destroy their ouytposts. You have at your disposal');
  95.    writeln('a cannon, which you can shoot at any angle.  As this is war,');
  96.    writeln('supplies are short, so you only have ',numshells,' per target.');
  97.    writeln;
  98.  
  99.    killed := 0;
  100.    repeat
  101.       writeln('*******************************************');
  102.       fire;
  103.       write('I see another one, care to shoot again? ');
  104.       readln(ch);
  105.       while not (ch in ['y','n','Y','N']) do
  106.       begin
  107.          writeln('Please answer yes or no');
  108.          write('Want to try again? ');
  109.          readln(ch)
  110.       end
  111.    until (ch<>'y') and (ch<>'Y');
  112.    writeln;
  113.    writeln('You killed ',killed,' of the enemy.')
  114. end.
  115.